package com.zaimokuza.cn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.message.AlertMessage;
import com.zaimokuza.cn.entity.message.ContactUserUnreadNumber;
import com.zaimokuza.cn.entity.message.Message;
import com.zaimokuza.cn.entity.message.vo.MessageInfo;
import com.zaimokuza.cn.mapper.MessageAlertMapper;
import com.zaimokuza.cn.mapper.MessageMapper;
import com.zaimokuza.cn.service.MessageService;
import com.zaimokuza.cn.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

import static com.zaimokuza.cn.entity.constants.UserConstant.ALERT_ADMIN_ID;

/**
 * @author zaimokuza
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private UserService userService;
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private MessageAlertMapper messageAlertMapper;

    @Override
    public void send(Message message) {
        // 当前用户ID
        String userId = message.getSenderId();
        String friendId = message.getReceiverId();
        // 生成联络人对应的消息
        Message friendMessage = new Message();
        BeanUtils.copyProperties(message, friendMessage);
        // 设置原消息及联络人消息的剩余信息
        message.setUserId(userId);
        message.setFriendId(friendId);
        friendMessage.setUserId(friendId);
        friendMessage.setFriendId(userId);
        // 整理数组批量存储
        ArrayList<Message> messages = new ArrayList<>();
        messages.add(message);
        messages.add(friendMessage);
        messageMapper.insertBatchRecord(messages);
    }

    /**
     * 举报及处理举报的消息模板
     */
    private final static String ALERT_MESSAGE_TEMPLATE = "举报用户[%s]，举报原因：%s";
    private final static String HANDLE_ALERT_MESSAGE_TEMPLATE = "已处理关于用户[%s]的举报，处理结果：%s";

    @Override
    public void alert(AlertMessage alertMessage) {
        // 当前用户ID
        String userId = alertMessage.getSenderId();
        // 组织消息对象
        Message message = new Message();
        message.setSenderId(userId);
        message.setReceiverId(ALERT_ADMIN_ID);
        message.setUserId(userId);
        message.setFriendId(ALERT_ADMIN_ID);
        message.setContent(String.format(ALERT_MESSAGE_TEMPLATE, userService.getUserNameById(alertMessage.getUserId()), alertMessage.getContent()));
        // 添加消息记录
        ArrayList<Message> messages = new ArrayList<>();
        messages.add(message);
        messageMapper.insertBatchRecord(messages);
        // 添加举报记录
        messageAlertMapper.insertRecord(alertMessage);
    }

    @Override
    public void handleAlert(AlertMessage alertMessage) {
        // 举报人ID
        String senderId = alertMessage.getSenderId();
        // 组织消息对象
        Message message = new Message();
        message.setSenderId(ALERT_ADMIN_ID);
        message.setReceiverId(senderId);
        message.setUserId(senderId);
        message.setFriendId(ALERT_ADMIN_ID);
        message.setContent(String.format(HANDLE_ALERT_MESSAGE_TEMPLATE, userService.getUserNameById(alertMessage.getUserId()), alertMessage.getHandleContent()));
        // 添加消息记录
        ArrayList<Message> messages = new ArrayList<>();
        messages.add(message);
        messageMapper.insertBatchRecord(messages);
        // 修改举报记录
        messageAlertMapper.updateRecord(alertMessage);
    }

    @Override
    public void updateMessageStatus(String userId, String friendId) {
        messageMapper.updateBatchRecordStatus(userId, friendId);
    }

    @Override
    public PageResult<MessageInfo> getMessagePage(String userId, String friendId, Integer page, Integer size) {
        // 分页
        PageHelper.startPage(page, size);
        // 查询
        Page<MessageInfo> messageInfoPage = messageMapper.queryMessagePage(userId, friendId);
        // 封装结果并返回
        return PageResult.from(messageInfoPage);
    }

    @Override
    public PageResult<AlertMessage> getAlterMessagePage(Boolean isHandle, Integer page, Integer size) {
        // 分页
        PageHelper.startPage(page, size);
        // 查询
        Page<AlertMessage> alertMessagePage = messageAlertMapper.queryAlterMessagePage(isHandle);
        // 封装结果并返回
        return PageResult.from(alertMessagePage);
    }

    @Override
    public HashMap<String, Long> getContactUserUnreadNumber(String userId, ArrayList<String> contactUserIdList) {
        // 查询联系人未读消息数量
        ArrayList<ContactUserUnreadNumber> contactUserUnreadNumbers = messageMapper.queryContactUserUnreadNumber(userId, contactUserIdList);
        // 将对象数组整理为Map集合
        HashMap<String, Long> contactUserUnreadNumberMap = new HashMap<>(contactUserUnreadNumbers.size());
        for (ContactUserUnreadNumber contactUserUnreadNumber : contactUserUnreadNumbers) {
            contactUserUnreadNumberMap.put(contactUserUnreadNumber.getFriendId(), contactUserUnreadNumber.getUnreadNumber());
        }
        // 返回结果集合
        return contactUserUnreadNumberMap;
    }

}
